-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for json output format #49
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contribution! Please check one comment above. Also, please add an example to _examples/
with json output, and build it using _examples/build-examples.sh
.
render/templates.go
Outdated
@@ -31,6 +32,10 @@ var tplFuncs = map[string]any{ | |||
} | |||
return sum | |||
}, | |||
"marshal": func(v any) string { | |||
a, _ := json.Marshal(v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like here a fatal error can occur. We need to panic here and recover on render caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can return an error along with string result instead of panic.
- add an example for json format - update marshal function to return an error
Thanks for your feedback! I've added json format to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it looks good. Could you check please some comments with questions?
_examples/simple/doc.json
Outdated
@@ -0,0 +1 @@ | |||
[{"env_name":"HOST","doc":"Hosts name of hosts to listen on.","env_default":"","env_separator":";","required":true,"expand":false,"non_empty":false,"from_file":false},{"env_name":"PORT","doc":"Port to listen on.","env_default":"","env_separator":"","required":true,"expand":false,"non_empty":true,"from_file":false},{"env_name":"DEBUG","doc":"Debug mode enabled.","env_default":"false","env_separator":"","required":false,"expand":false,"non_empty":false,"from_file":false},{"env_name":"PREFIX","doc":"Prefix for something.","env_default":"","env_separator":"","required":false,"expand":false,"non_empty":false,"from_file":false}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be more readable with indentations (it could be set in json encoder), it seems here we don't need size optimization, since the size of the documentation can't be big enough.
And what do you think about omitting empty fields? E.g. envDefault
and envSeparator
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I've added omitempty
tag for all bool fields as well.
Sometimes it needs to export documentation for envs in JSON format for further automation.
This PR supports a new output format
json
.